import wtforms
from wtforms.validators import Email, Length, EqualTo, InputRequired
from models import UserModel
from flask import Blueprint, render_template, jsonify, redirect, url_for, session
#from exts import db


# Form：主要就是用来验证前端提交的数据是否符合要求
class RegisterForm(wtforms.Form):

    email = wtforms.StringField(validators=[Email(message="邮箱格式错误！")])
    #这些字段和html的标签名一致就可以了
    # 自定义验证：
    # 1. 邮箱是否已经被注册
    def validate_email(self, field):
        email = field.data
        user = UserModel.query.filter_by(email=email).first()
        if user:
            session['isre'] = 1
            session['email']=field.data
            raise wtforms.ValidationError(message="该用户已经被注册！")

class checkform(wtforms.Form):
    check = wtforms.StringField(validators=[Length(min=4, max=4, message="验证码格式错误！")])

    def validate_check(self, field):
        check = field.data
        print("用户输入的验证码是：",check)
        if check!=session.get('check'):
            raise wtforms.ValidationError(message="验证码错误！")

class PForm(wtforms.Form):
    m_pwd1 = wtforms.StringField(validators=[Length(min=6, max=20, message="密码格式错误！")])
    m_pwd2 = wtforms.StringField(validators=[EqualTo("m_pwd1", message="两次密码不一致！")])

    '''
 class PForm(wtforms.Form):
    m_pwd1 = wtforms.StringField(validators=[Length(min=6, max=20, message="密码格式错误！")])
    m_pwd2 = wtforms.StringField(validators=[EqualTo("m_pwd1", message="两次密码不一致！")])


    


class LoginForm(wtforms.Form):
    email = wtforms.StringField(validators=[Email(message="邮箱格式错误！")])
    password = wtforms.StringField(validators=[Length(min=6, max=20, message="密码格式错误！")])


class QuestionForm(wtforms.Form):
    title = wtforms.StringField(validators=[Length(min=3, max=100, message="标题格式错误！")])
    content = wtforms.StringField(validators=[Length(min=3,message="内容格式错误！")])


class AnswerForm(wtforms.Form):
    content = wtforms.StringField(validators=[Length(min=3, message="内容格式错误！")])
    question_id = wtforms.IntegerField(validators=[InputRequired(message="必须要传入问题id！")])
'''